package f5transformation.aggregation;

import flinkemp.Emp;
import flinkemp.EmpFun;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class KeyBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Emp> data = env.addSource(new EmpFun());
        data.print();
        KeyedStream<Emp, Integer> keyData = data.keyBy(new KeySelector<Emp, Integer>() {
            @Override
            public Integer getKey(Emp emp) throws Exception {
                return emp.deptNo;
            }
        });
        keyData.sum("sal").print("按部门编号求薪资和");
        env.execute();
    }
}
